package com.fjxd.people.service.impl;

import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjxd.feign.result.ViewResult;
import com.fjxd.people.mapper.PeopleMapper;
import com.fjxd.people.pojo.People;
import com.fjxd.people.service.PeopleService;
import com.fjxd.people.utils.NativePlace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PeopleServiceImpl implements PeopleService {


    @Autowired
    private PeopleMapper peopleMapper;
    /*
     * 添加购票人
     * */
    @Override
    public ViewResult savePeople(Integer id, People people) {
        People p=new People();
        String carId=people.getCardId();
        p.setCardId(carId);
        boolean validCard = IdcardUtil.isValidCard(carId);
        if(validCard)
        {
            p.setNickname(people.getNickname());
            if (people.getSex()!=null)
            {
                p.setSex(people.getSex());
            }
            else {
                p.setSex("未知");
            }
            // 获取年龄
            String age=String.valueOf(IdcardUtil.getAgeByIdCard(carId));
            p.setAge(age);
            p.setCountry(people.getCountry());
            // 获取常驻省份
            NativePlace n=new NativePlace();
            String address= IdcardUtil.getProvinceByIdCard(carId)+"省"+n.getNameString(Integer.parseInt(carId.substring(0,4)))+n.getNameString(Integer.parseInt(carId.substring(0, 6)));
            p.setAddress(address);
            p.setPeopleType(people.getPeopleType());
            String phone=people.getPhone();
            if (phone !=null)
            {
                p.setPhone(phone);
            }
            else {
                p.setPhone("无号码");
            }
            p.setUserId(id);
            peopleMapper.insert(p);
            return ViewResult.success(p);
        }
        else
        {
            return new ViewResult(ViewResult.Status.IDCARD.getCode(), ViewResult.Status.IDCARD.getMessage());
        }

    }

    /*
     * 删除购票人
     * */
    @Override
    public ViewResult deletePeople(Integer id) {        
        return ViewResult.success(peopleMapper.deleteById(id));
    }

    /*
     * 修改购票人
     * */
    @Override
    public ViewResult updatePeople(Integer id, People people) {
        People p = new People();
        NativePlace n = new NativePlace();

        p.setAddress(people.getAddress());
        p.setPhone(people.getPhone());
        p.setPeopleType(people.getPeopleType());
        p.setId(id);
        return ViewResult.success(peopleMapper.updateById(p));
    }

    /*
     * 查找账号下的所有人员信息
     * */
    @Override
    public List<People> getPeopleForUser(Integer userId) {
        LambdaQueryWrapper<People> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(People::getUserId,userId);
        List<People> people=peopleMapper.selectList(queryWrapper);
        for(People p:people)
        {
            String a=peopleType(Integer.parseInt(p.getPeopleType()));
            p.setPeopleType(a);
        }

        return people;
    }
    /*
    * 查询单个人员
    * */
    @Override
    public People getPeople(Integer id) {
        LambdaQueryWrapper<People> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(People::getId,id);
        People people = peopleMapper.selectOne(queryWrapper);
        if(people!=null)
        {
            return people;
        }
        return null;
    }

    /*
     * 人员类型
     * */
    public String peopleType(int p){
        switch (p)
        {
            case 0:
                return "普通群众";
            case 1:
                return "学生";
            case 2:
                return "退役士兵";
        }
        return "未知";
    }
}
